﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilityClassLIB;

namespace AttributesClassNameSpace
{
    public class AttributesClass
    {
        UtilityClass utility_obj = new UtilityClass();//creates utility class object calls utilty_obj
        String connectionString = UtilityClass.ConnectionString();//calls method connectionstring from utility class

        //search is for attribute for name entered on wpf, returns datarow with user info
        public DataRow[] SearchAttributebyName(string attributename)
        {
            string selectBy = "attributeName = '" + attributename + "'";
            DataTable dt = utility_obj.fillDataTable("tbAttributes");
            DataRow[] row_result = dt.Select(selectBy);

            return row_result;
        }

        //inserts new attribute into database using input from user
        public string InsertRowinAttributeTable(string an, string at, string adp, string ad, string dbt)
        {
            string result = "";
            int uid = 0;

            //gets current attributes to create unique attributeID (has to be a better way to do this?)
            DataTable dt = utility_obj.fillDataTable(dbt);
            DataRow[] dr = dt.Select();

            //creates unique user id by finding highest attribute id and adding 1
            foreach (DataRow row in dt.Rows)
            {
                uid = row.Field<int>("attributeID");
                int maxUID = Math.Max(Int32.MaxValue, uid);
            }
            uid = uid + 1;

            //inserts row into user table
            SqlConnection connection = new SqlConnection(connectionString);

            try
            {
                using (SqlCommand insertCommand = new SqlCommand(
                "INSERT INTO dbo.tbAttributes VALUES(@attributeID, @attributeName, @attributeType, @attributeDescription, @attributeDisabled ) ", connection))
                {
                    insertCommand.Parameters.AddWithValue("@attributeID", uid);
                    insertCommand.Parameters.AddWithValue("@attributeName", an);
                    insertCommand.Parameters.AddWithValue("@attributeType", at);
                    insertCommand.Parameters.AddWithValue("@attributeDescription", adp);
                    insertCommand.Parameters.AddWithValue("@attributeDisabled", ad);

                    insertCommand.Connection = connection;
                    connection.Open();
                    insertCommand.ExecuteNonQuery();
                    result = "New Attribute Added: " + Convert.ToString(uid) + " " + an + " " + at + " " + adp + " " + ad;
                }
            }//end try
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                result = sqlException.Message;
            }
            catch (Exception e)
            {
                result = "Opps! Something went wrong. " + e.Message;
            }
            return result;
        }// end insertattributerow


// update row in attribute tables
        public string EditRowinAttributeTable(string aid, string an, string at, string ad, string adp)
        {
            string result = "";

            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                using (SqlCommand insertCommand = new SqlCommand(
                "UPDATE dbo.tbAttributes SET  attributeName=@AttributeName, attributeType = @attributeType, attributeDescription = @attributeDescription, attributeDisabled = @attributeDisabled where attributeID ='" + aid + "'", connection))
                {

                    insertCommand.Parameters.AddWithValue("@attributeName", an);
                    insertCommand.Parameters.AddWithValue("@attributeType", at);
                    insertCommand.Parameters.AddWithValue("@attributeDisabled", ad);
                    insertCommand.Parameters.AddWithValue("@attributeDescription", adp);

                    insertCommand.Connection = connection;
                    connection.Open();
                    insertCommand.ExecuteNonQuery();
                    result = "Success! Attribute Updated: " + aid + " " + at + " " + adp + " " + ad;
                }
            }//end try
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                result = sqlException.Message;
            }
            catch (Exception e)
            {
                result = "Opps! Something went wrong. " + e.Message;
            }
            return result;
        }// end edit attribute

    }//end attributes class
}//end namespace
